package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.Strategy;
import cn.wolfcode.trip.base.domain.StrategyDetail;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.query.StrategyDetailQueryObject;
import cn.wolfcode.trip.base.service.IStrategyDetailService;
import cn.wolfcode.trip.base.util.UploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("/strategyDetail")
public class StrategyDetailController {
    @Autowired
    private IStrategyDetailService strategyDetailService;

    @RequestMapping("list")
    public String list(Model m,@ModelAttribute("qo") StrategyDetailQueryObject qo){
        qo.setOrderBy("sd.releaseTime desc");
        m.addAttribute("pageInfo",strategyDetailService.query(qo));
        return "/strategyDetail/list";
    }
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(StrategyDetail strategyDetail, MultipartFile file){
        if ((!file.isEmpty()) && file.getSize() > 0) {
            String url = UploadUtil.upload(file);
            strategyDetail.setCoverUrl(url);
        }
        strategyDetailService.saveOrUpdate(strategyDetail);
        return "redirect:/strategyDetail/list.do";
    }

    @RequestMapping("/getContentById")
    @ResponseBody
    public Object getContentById(Long strategyDetailId){
        return strategyDetailService.getContentById(strategyDetailId);
    }


}
